define(function(require, exports) {
	var $ = require('jquery');
	var pubjs = require('pubjs');
	var util = require('util');
	var view = require('@base/view');

	// 渠道路径模块
	var Base = view.container.extend({
		init: function(config) {
			config = pubjs.conf(config, {
				'class': 'M-channelPath',
				'url': '',
				'param': null,
				'auto_load': true,
				"reqType": "websocket",		     // 默认通信方式使用ajax，可选websocket
				"reqMethod":"get",		     // 数据获取方式

				'xField': {           // 路径数据
					'field': 'path',
					'format': null,
					'sub': {
						'xField': {
							'field': 'name',
							'format': null
						},
						'yField': {
							'field': 'value',
							'format': null
						}
					}
				},

				'yField': {           // 指标数据
					'field': 'value',
					'format': null
				}
			});

			this.Super('init', arguments);
		},
		afterBuild: function() {
			var c = this.getConfig();
			this.reset();
			if (c.auto_load && c.url) {
				this.load();
			}
		},
		reset: function() {
			this.$items = [];
			this.$data = null;
			this.$el.empty();
			return this;
		},
		setParam: function(param, replace) {
			var c = this.getConfig();
			c.param = replace ? param : util.extend(c.param, param);
			return this;
		},
		load: function() {
			var c = this.getConfig();
			if (c.url) {
				pubjs.loading.show();
				switch(c.reqType){
					case 'ajax':
						pubjs.data[c.reqMethod](c.url, c.param, this, 'onData');
					break;
					case 'websocket':
						pubjs.mc.send(c.url, c.param, this.onData.bind(this));
					break;
				}
			}

			return this;
		},
		onData: function(err, data) {
			pubjs.loading.hide();

			if (err) {
				return pubjs.alert(err.message);
			}

			this.setData(data || {});
		},
		getData: function() {
			return this.$data;
		},
		setData: function(data) {
			this.$data = data;
			var c = this.getConfig();
			var container = this.getDOM();
			var xField = c.xField;
			var yField = c.yField;
			yField = util.isArray(yField) ? yField[0] : yField;
			var xFieldKey = util.isString(xField) ? xField : xField.field;
			var yFieldKey = util.isString(yField) ? yField : yField.field;

			// 构建第一层
			util.each(data && data.items, function(itemData) {
				var xValue = itemData[xFieldKey];
				var yValue = itemData[yFieldKey];

				if (util.isFunc(xField.format)) {
					xValue = xField.format.call(this, xValue, itemData, c) || '-';
				}

				if (util.isFunc(yField.format)) {
					yValue = yField.format.call(this, yValue, itemData, c) || [];
				}

				var elm = $([
					'<div class="M-channelPathItem">',
						'<em class="M-channelPathItemSerial"></em>',
						'<div class="M-channelPathItemMetric"></div>',
						'<div class="M-channelPathItemPath"></div>',
					'</div>'
				].join('')).appendTo(container);

				var item = {
					data: itemData,
					elm: elm,
					serial: elm.find('.M-channelPathItemSerial'),
					metric: elm.find('.M-channelPathItemMetric'),
					path: elm.find('.M-channelPathItemPath'),
					children: []
				};
				this.$items.push(item);

				item.serial.text(this.$items.length + '.');
				item.metric.text(yValue);



				// 构建路径 （第二层）
				util.each(xValue, function(subData) {
					var subField = xField.sub;
					var subXField = subField.xField;
					var subYField = subField.yField;
					subYField = util.isArray(subYField) ? subYField[0] : subYField;
					var subXFieldKey = util.isString(subXField) ? subXField : subXField.field;
					var subYFieldKey = util.isString(subYField) ? subYField : subYField.field;

					var subXValue = subData[subXFieldKey];
					var subYValue = subData[subYFieldKey];

					if (util.isFunc(subXField.format)) {
						subXValue = subXField.format.call(this, subXValue, subData, c) || '-';
					}

					if (util.isFunc(subYField.format)) {
						subYValue = subYField.format.call(this, subYValue, subData, c) || [];
					}

					var subElm = $([
						'<div class="M-channelPathSubItem">',
							'<div class="M-channelPathSubItemLabel"></div>',
							'<div class="M-channelPathSubItemValue"></div>',
						'</div>'
					].join('')).appendTo(item.path);

					var subItem = {
						data: subData,
						elm: subElm,
						label: subElm.find('.M-channelPathSubItemLabel'),
						value: subElm.find('.M-channelPathSubItemValue')
					};
					item.children.push(subItem);

					subItem.label.text(subXValue);
					subItem.value.text(subYValue);
				}, this);
			}, this);

			return this;
		}
	});
	exports.base = Base;
});